#ifndef WINDOW_H
#define WINDOW_H

#include <QMainWindow>
#include "SketchWidget.h"
#include <QKeyEvent>
#include <Communication.h>
#include <AiboWalkControl.h>
#include <AiboHeadControl.h>
#include <AiboState.h>
#include <WalkInstructionHandler.h>

namespace Ui {
class Window;
class KeyboardControl;
}

struct KeyPressTimer {
	bool isKeyPressed;
	int m_key;
	QTimer* keyTimer;
};

struct WalkControlTimerList {
	KeyPressTimer* forwardWalk;
	KeyPressTimer* backwardWalk;
	KeyPressTimer* turnLeftWalk;
	KeyPressTimer* turnRightWalk;
	KeyPressTimer* strafeLeftWalk;
	KeyPressTimer* strafeRightWalk;
};

struct HeadControlTimerList {
	KeyPressTimer* rollUp;
	KeyPressTimer* rollDown;
	KeyPressTimer* panLeft;
	KeyPressTimer* panRight;
	KeyPressTimer* tiltUp;
	KeyPressTimer* tiltDown;
};

class Window : public QMainWindow
{
    Q_OBJECT
    
public:
    Window(QWidget *parent = 0);
    ~Window();

    //Sketch based
    void doBodyRotation(double angle);

protected:
    void keyPressEvent( QKeyEvent *event );
    void keyReleaseEvent( QKeyEvent *event );
	void mouseMoveEvent(QMouseEvent *event);

private slots:
	void initAibo();
    void updateCamera();
	void updateWalkInstructions();
	void updateHeadInstructions();

	void checkWalkControlKeys();
	void checkHeadControlKeys();

    //Sketch based
    void doOneStep();
    void doAll();
	void connectToAibo();
    void doRotate();
    void doForward();
    void finishRotate();
    void finishForward();

private:
	void disconnectAibo();
	bool checkWalkKeyPressed( QKeyEvent *event );
	bool checkWalkKeyReleased( QKeyEvent *event );
	bool checkHeadKeyReleased( QKeyEvent *event );
	bool checkHeadKeyPressed( QKeyEvent *event );
	void setWalkKeyTimers();
	void setHeadKeyTimers();

	void setKeyTimer( KeyPressTimer* kTimer, int keyPressed, QTimer* timer );

	void startWalkKeyTimer( KeyPressTimer* kTimer );
	void startHeadKeyTimer( KeyPressTimer* kTimer );
	void stopWalkKeyTimer( KeyPressTimer* kTimer );
	void stopHeadKeyTimer( KeyPressTimer* kTimer );
	void checkWalkKeyTimers();
	void checkHeadKeyTimers();

    void updateHeadStateWindow();

    //sketch based methods
    void createInstructionsForFirstPoint();
    void createInstructionsForAllPoint();
    void createInstructionRotation(double angle);
    void readInstructions();
    void doInstruction(WalkForwardInstruction* I);
    void doInstruction(WalkRotateInstruction* I);
    void finishInstruction(WalkForwardInstruction* I);
    void finishInstruction(WalkRotateInstruction* I);
    int angleToDuration(float angle);
    int distanceToDuration(float distance);

    SketchWidget* m_sketch;
	AiboState *m_aiboState;
	WalkInstructionHandler *m_walkHandler;
	HeadInstructionHandler *m_headHandler;
    AiboHeadControl *aiboHead;
    AiboWalkControl *aiboWalk;
    Communication *com;
    Ui::Window *ui;
	QTimer *waitForConnect;
    QTimer *camera;
	QTimer *aiboWalkTimer;
	QTimer *aiboHeadTimer;
	QTimer *keyWalkTimer;
	QTimer *keyHeadTimer;
	WalkControlTimerList walkKeyList;
	HeadControlTimerList headKeyList;
    QWaitCondition doSleep;
	QCursor* qCursor;

    //For sketch based
    QVector<WalkInstruction *> listInstruction;
    QTimer* walkForwardTimer;
    QTimer* walkRotateTimer;
    WalkInstruction* currentInstruction;
    bool playButton;
};


#endif // WINDOW_H
